{% template "admin/header.html" %}
<style>
    .layui-table-cell {
        font-size: 13px;
        color: #333;
    }
</style>
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>文件管理</legend>
    </fieldset>
    <div class="demoTable">
        <select id="keys" style="height: 35px;padding: 0 5px;">
            <option value="title">标题</option>
            <option value="keywords">关键字</option>
        </select>
        <div class="layui-inline">
            <input class="layui-input" name="key" id="values" placeholder="请输入关键字">
        </div>
        <button class="layui-btn" id="search" data-type="reload">搜索</button>
    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>

{% template "admin/footer.html" %}

<script type="text/html" id="action">
    <a href="/admin/upload_file/download?id={{d.Id}}" class="layui-btn layui-btn-xs" >下载</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="pic">
    {{# if(d.Picture){ }}
    <img src="{{d.Picture}}" style="max-height: 50px;cursor: pointer;"  onclick="imgEn(this)" />
    {{# } }}
</script>

<script type="text/html" id="lt">
    {{# if(d.Local_type == 1){ }}
        <span>本地</span>
    {{# }else if(d.Local_type == 2){ }}
        <span>网络</span>
    {{# }else if(d.Local_type == 3){ }}
        <span>其他</span>
    {{# } }}
</script>
<script type="text/html" id="tys">
    {{getType(d.Type)}}
</script>

<script type="text/html" id="size">
    {{getFileSize(d.Size)}}
</script>
<script type="text/html" id="at">
    {{formatDate(d.Add_time)}}
</script>
<script type="text/html" id="pre">
    {{preView(d)}}
</script>
<script>

    let $ = null;
    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form; $ = layui.jquery;
        var tableIn = table.render({
            id: 'message',
            elem: '#list',
            url: '/admin/upload_file/query',
            where:{},
            method: 'post',
            page:true,
            cols: [[
                {field: 'Id', title: 'ID', width: 80},
                {title: '预览', width: 150,toolbar:'#pre'},
                {field: 'Upload_name', title: '源文件名', width: 200},
                {field: 'File_name', title: '存储文件名', width: 200},
                {field: 'Type_name', title: '分类', width: 140},
                {title: '类型', width: 110,toolbar:'#tys'},
                {field: 'Ext', title: '扩展名', width: 100},
                {field: 'Add_time', title: '保存时间', width: 160,toolbar:'#at'},
                {field: 'Size', title: '大小', width: 110,toolbar:"#size"},
                {field: 'Local_type', title: '位置', width: 100,toolbar:"#lt"},
                { align: 'center', toolbar: '#action',width:150}
            ]],
            limit:10
        });
        //搜索
        $('#search').on('click', function () {
            var key = $('#keys').val();
            var values = $("#values").val();
            if ($.trim(values) === '') {
                layer.msg('请输入关键字！', {icon: 0});
                return;
            }
            var p={};
            p[key] = values;
            tableIn.reload({ page: {page: 1}, where: p});
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('您确定要删除该文件吗？', function(index){
                    loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("/admin/upload_file/delete",{id:data.Id},function(res){
                        layer.close(loading);
                        if(res.Code===1){
                            layer.msg(res.Msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });


    });

    function getType(t) {
        let str = "";
        switch (t) {
            case 0:
                str = "未知";
                break;
            case 1:
                str = "图片";
                break;
            case 2:
                str = "文档";
                break;
            case 3:
                str = "视频";
                break;
            case 4:
                str = "音频";
                break;
            case 5:
                str = "压缩包";
                break;
        }
        return str;
    }

    //预览
    function preView(da) {
        let str = "";
        let t = da.Type;
        switch (t) {
            case 0:
                str = "<a href='"+da.Url+"' style='color: blue;'>未知</a>";
                break;
            case 1:
                str = "<img src='"+da.Url+"' onclick='imgEn(this)' style='max-height: 55px;cursor: pointer' />";
                break;
            case 2:
                str = "<a href='"+da.Url+"' style='color: blue;'>文档</a>";
                break;
            case 3:
                str = "<a href='"+da.Url+"' style='color: blue;'>视频</a>";
                break;
            case 4:
                str = "<a href='"+da.Url+"' style='color: blue;'>音频</a>";
                break;
            case 5:
                str = "<a href='"+da.Url+"' style='color: blue;'>压缩包</a>";
                break;
        }
        return str;
    }
    //文件大小
    function getFileSize(fileByte) {
        var fileSizeByte = fileByte;
        var fileSizeMsg = "";
        if (fileSizeByte < 1048576) fileSizeMsg = (fileSizeByte / 1024).toFixed(2) + "KB";
        else if (fileSizeByte == 1048576) fileSizeMsg = "1MB";
        else if (fileSizeByte > 1048576 && fileSizeByte < 1073741824) fileSizeMsg = (fileSizeByte / (1024 * 1024)).toFixed(2) + "MB";
        else if (fileSizeByte > 1048576 && fileSizeByte == 1073741824) fileSizeMsg = "1GB";
        else if (fileSizeByte > 1073741824 && fileSizeByte < 1099511627776) fileSizeMsg = (fileSizeByte / (1024 * 1024 * 1024)).toFixed(2) + "GB";
        else fileSizeMsg = "文件超过1TB";
        return fileSizeMsg;
    }

    /**
     * 时间戳转换日期
     * @param now
     * @returns {string}
     */
    function formatDate(time) {
        time = parseInt(time);
        time = (time * 1000) + 8 * 3600 * 1000;
        var date = new Date(time);
        return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '-');
    }

    //图片放大
    function imgEn(_this){ //传入捕获元素放大后的宽高,str为所需放大图片的路径
        var $str = '<img src="'+$(_this).attr('src')+'" style="width: auto;max-width:100%;height: auto;max-height: 100%;" />';
        var $width ='auto;max-width:98%;';
        var $hight = 'auto;max-height:98%;';
        layer.open({
            type: 1,
            shade: 0.3,
            shadeClose:true,
            title: false,
            area: [$width, $hight],
            content: $str,
            cancel: function(index){
                layer.close(index);
            }
        });
    }
</script>
</body>
</html>